package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.EcologyPoolSourceDay;
import com.sc.nft.mapper.EcologyPoolSourceDayMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author 刘国航
 * @since 2024/7/15
 */
@Slf4j
@Repository
public class EcologyPoolSourceDayDao extends ServiceImpl<EcologyPoolSourceDayMapper, EcologyPoolSourceDay> {

    public Page<EcologyPoolSourceDay> sourceDayPage(Page<EcologyPoolSourceDay> page) {
        LambdaQueryWrapper<EcologyPoolSourceDay> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(EcologyPoolSourceDay::getLinkDate);
        return page(page, queryWrapper);
    }

    public EcologyPoolSourceDay getEcologyPoolSourceDay(Date date) {
        LambdaQueryWrapper<EcologyPoolSourceDay> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EcologyPoolSourceDay::getLinkDate,date);
        wrapper.orderByDesc(EcologyPoolSourceDay::getId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public void updateDateLinkCount(Long id, BigDecimal linkCount) {
        LambdaUpdateWrapper<EcologyPoolSourceDay> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(EcologyPoolSourceDay::getId,id);
        wrapper.setSql("link_count = link_count + "+linkCount);
        update(new EcologyPoolSourceDay(),wrapper);
    }
}
